<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>歌词解析</title>
</head>
<body>



<script>
	var songLyric = "[00:04.050]\n[00:12.570]难以忘记初次见你\n[00:16.860]一双迷人的眼睛\n[00:21.460]在我脑海里\n[00:23.960]你的身影 挥散不去\n[00:30.160]握你的双手感觉你的温柔\n[00:34.940]真的有点透不过气\n[00:39.680]你的天真 我想珍惜\n[00:43.880]看到你受委屈 我会伤心\n[00:48.180]喔\n[00:50.340]只怕我自己会爱上你\n[00:55.070]不敢让自己靠的太近\n[00:59.550]怕我没什么能够给你\n[01:04.030]爱你也需要很大的勇气\n[01:08.190]只怕我自己会爱上你\n[01:12.910]也许有天会情不自禁\n[01:17.380]想念只让自己苦了自己\n[01:21.840]爱上你是我情非得已\n[01:28.810]难以忘记初次见你\n[01:33.170]一双迷人的眼睛\n[01:37.700]在我脑海里 你的身影 挥散不去\n[01:46.360]握你的双手感觉你的温柔\n[01:51.120]真的有点透不过气\n[01:55.910]你的天真 我想珍惜\n[02:00.150]看到你受委屈 我会伤心\n[02:04.490]喔\n[02:06.540]只怕我自己会爱上你\n[02:11.240]不敢让自己靠的太近\n[02:15.750]怕我没什么能够给你\n[02:20.200]爱你也需要很大的勇气\n[02:24.570]只怕我自己会爱上你\n[02:29.230]也许有天会情不自禁\n[02:33.680]想念只让自己苦了自己\n[02:38.140]爱上你是我情非得已\n[03:04.060]什么原因 耶\n[03:07.730]我竟然又会遇见你\n[03:13.020]我真的真的不愿意\n[03:16.630]就这样陷入爱的陷阱\n[03:20.700]喔\n[03:22.910]只怕我自己会爱上你\n[03:27.570]不敢让自己靠的太近\n[03:32.040]怕我没什么能够给你\n[03:36.560]爱你也需要很大的勇气\n[03:40.740]只怕我自己会爱上你\n[03:45.460]也许有天会情不自禁\n[03:49.990]想念只让自己苦了自己\n[03:54.510]爱上你是我情非得已\n[03:58.970]爱上你是我情非得已\n[04:03.000]\n";

	

	

	// var shuzu2 = decodeLyric(songLyric);
	// console.log(shuzu2);

var ceshi = "[00:04.050]\n[00:12.570][00:13.570][00:14.570]难以忘记初次见你\n[00:16.860]一双迷人的眼睛\n";
var ceshi1 = ["[00:04.050]","[00:12.570][00:13.570][00:14.570]难以忘记初次见你","[00:16.860]一双迷人的眼睛",""];
	
	function decodeLyric(lyricOrigin){
		var lyric = lyricOrigin.split("\n");
		var arr = [];

		lyric.map(function(val,ind){
			if(ind !== lyric.length - 1){
				
				if(val.indexOf("]") !== val.lastIndexOf("]")){
					var lyricAndText = val.split("]");

					for(let i = 0;i<lyricAndText.length-1;i++){
						let tem = lyricAndText[i]+"]" + lyricAndText[lyricAndText.length-1];
						arr.push(tem);
					}
				}else{
					arr.push(val);
				}
		}
	});
		var shuzu = [];
		var shuzuchengdu = arr.length;
		arr.map(function(val,ind){
		
			var obj = {};
			var lyricAndText = val.split("]");
			var lyricAndText2  = [].concat(lyricAndText[0]+"]",lyricAndText[1]);
	
			var dw1 = lyricAndText2[0].indexOf("[");
			var dw2 = lyricAndText2[0].indexOf(":");
			var dw3 = lyricAndText2[0].indexOf(".");
			var dw4 = lyricAndText2[0].indexOf("]");
	
			var fen = lyricAndText2[0].substr(dw1+1,2)*1;
			var miao = lyricAndText2[0].substr(dw2+1,2)*1
			var haomiao = lyricAndText2[0].substring(dw3+1,dw4)*1;
			var totalTime = fen*60*1000 + miao*1000 + haomiao;

			//这个判断NAN的情况
			if(totalTime !== totalTime){
				totalTime = 0;
			}
	
			obj.lineNum = ind;
			obj.time = totalTime;
			obj.txt = lyricAndText2[1];
	
			shuzu.push(obj);
			shuzu.sort(function(a,b){
				return a.time - b.time;
			});
			
		
		});

		//为了避免混乱,当一个元素time不为0的时候,帮它补一个
		if(shuzu[0].time !== 0){
			let addZero = {
				lineNum:0,
				time:0,
				txt: ""
			}
			shuzu.unshift(addZero);
			//因为最前面添加了一个,这时候后面的lineNum索引应该全部都加1
			shuzu.map(function(val,ind){
				if(ind >0){
					return val.lineNum++;
				}
			});
		}
		return shuzu;
}

decodeLyric(ceshi);
	







</script>
</body>
</html>